package com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceMain.domain.FactoryProduceMain;
import com.ruoyi.develop.factory.factoryProduceMain.service.IFactoryProduceMainService;
import com.ruoyi.develop.factory.factoryProduceSale.domain.FactoryProduceSale;
import com.ruoyi.develop.factory.factoryProduceSale.service.IFactoryProduceSaleService;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.impl.GoodsSaleServiceImpl;
import com.ruoyi.develop.goodsSaledoGongxu.domain.GoodsSaledoGongxu;
import com.ruoyi.develop.goodsSaledoGongxu.service.IGoodsSaledoGongxuService;
import com.ruoyi.develop.goodsSaledoGongxuUser.domain.GoodsSaledoGongxuUser;
import com.ruoyi.develop.goodsSaledoGongxuUser.service.IGoodsSaledoGongxuUserService;
import com.ruoyi.develop.goodsSaledoGongxuUserLirun.domain.GoodsSaledoGongxuUserLirun;
import com.ruoyi.develop.goodsSaledoGongxuUserLirun.service.impl.GoodsSaledoGongxuUserLirunServiceImpl;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.impl.ProjectBaseInfoServiceImpl;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.mapper.GoodsSaledoGongxuUserMoneyMapper;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.domain.GoodsSaledoGongxuUserMoney;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.IGoodsSaledoGongxuUserMoneyService;

/**
 * 报工列Service业务层处理
 * @author qh
 * @date 2024-11-13
 */
@Service
public class GoodsSaledoGongxuUserMoneyServiceImpl implements IGoodsSaledoGongxuUserMoneyService {
    @Resource
    private GoodsSaledoGongxuUserMoneyMapper goodsSaledoGongxuUserMoneyMapper;
    @Autowired
    private IGoodsSaledoGongxuUserService goodsSaledoGongxuUserService;
    @Autowired
    private IGoodsSaledoGongxuService goodsSaledoGongxuService;
    @Autowired
    private GoodsSaleServiceImpl goodsSaleService;
    @Autowired
    private IFactoryProduceSaleAllService iFactoryProduceSaleAllService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService iSysDeptService;
    @Autowired
    private GoodsSaledoGongxuUserLirunServiceImpl goodsSaledoGongxuUserLirunService;
    @Autowired
    private ProjectBaseInfoServiceImpl projectBaseInfoService;
    @Autowired
    private IFactoryProduceSaleService iFactoryProduceSaleService;
    @Autowired
    private IFactoryProduceMainService iFactoryProduceMainService;
    /**
     *
     * 查询报工列
     * @param id 报工列主键
     * @return 报工列
     */
    @Override
    public GoodsSaledoGongxuUserMoney selectById(String id) {
        return goodsSaledoGongxuUserMoneyMapper.selectById(id);
    }

    /**
     * 查询报工列列表
     * @param entity 报工列
     * @return 报工列
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<GoodsSaledoGongxuUserMoney> selectList(GoodsSaledoGongxuUserMoney entity) {
        return goodsSaledoGongxuUserMoneyMapper.selectList(entity);
    }
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<GoodsSaledoGongxuUserMoney> listAndParent(GoodsSaledoGongxuUserMoney entity) {
        return goodsSaledoGongxuUserMoneyMapper.listAndParent(entity);
    }
    @Override
    public List<GoodsSaledoGongxuUserMoney> listByGongxuId(GoodsSaledoGongxuUserMoney entity) {
        return goodsSaledoGongxuUserMoneyMapper.listByGongxuId(entity);
    }
    @Override
    public GoodsSaledoGongxuUserMoney selectByGoodssaleId(String goodssaleId) {
        return goodsSaledoGongxuUserMoneyMapper.selectByGoodssaleId(goodssaleId);
    }
    /**
     * 新增报工列
     * @param entity 报工列
     * @return 结果
     */
    @Override
    public int insert(GoodsSaledoGongxuUserMoney entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsSaledoGongxuUserMoneyMapper.insert(entity);
    }

    @Override
    public int insertBaogong(GoodsSaledoGongxuUserMoney entity) {
        GoodsSaledoGongxuUser goodsSaledoGongxuUser =goodsSaledoGongxuUserService.selectById(entity.getParentId());
        goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().add(entity.getCount()));
        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getCount().subtract(goodsSaledoGongxuUser.getFinishCount()));
        if(goodsSaledoGongxuUser.getRemainCount().compareTo(BigDecimal.ZERO) > 0){
            goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_1);
        }else{
            goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_2);
        }
        if(entity.getCount().compareTo(goodsSaledoGongxuUser.getCount())>0){
            return 0;
        }
        goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
        if(goodsSaledoGongxu!=null){
            goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().add(entity.getCount()));
            goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().subtract(entity.getCount()));
            goodsSaledoGongxuService.update(goodsSaledoGongxu);
            /*GoodsSaledoGongxu goodsSaledoGongxuMin= goodsSaledoGongxuService.selectMinFinish(goodsSaledoGongxu.getSaleId());
            if(goodsSaledoGongxuMin!=null){
                if( goodsSaledoGongxuMin.getFinishCount() != null && !goodsSaledoGongxuMin.getFinishCount().equals(BigDecimal.ZERO) ) {
                    if (goodsSaledoGongxuMin.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                        FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuMin.getFinishCount());
                        factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuMin.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()).subtract(factoryProduceSaleAll.getDaizhijianCount()));
                        iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                    }
                }
            }*/
            GoodsSaledoGongxu goodsSaledoGongxuLast= goodsSaledoGongxuService.selectLastFinish(goodsSaledoGongxu.getSaleId());
            if(goodsSaledoGongxuLast!=null){
                if(goodsSaledoGongxuLast.getFinishCount()!=null) {
                    if (goodsSaledoGongxuLast.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                        FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuLast.getFinishCount());
                        factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuLast.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                        if( factoryProduceSaleAll.getFinishdoCount().compareTo(factoryProduceSaleAll.getCount())>=0){
                            factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_5);
                        }else{
                            factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_4);
                        }
                        iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                        List<FactoryProduceSaleAll> list=iFactoryProduceSaleAllService.selectHaveNoFinish(factoryProduceSaleAll.getProduceId());
                        FactoryProduceMain factoryProduceMain = iFactoryProduceMainService.selectById(factoryProduceSaleAll.getProduceId());
                        if(factoryProduceMain!=null){
                            if(list.size()>0){
                                factoryProduceMain.setStatus(BusinessConstants.PROD_STATUS_4);
                            }else{
                                factoryProduceMain.setStatus(BusinessConstants.PROD_STATUS_5);
                            }
                        }
                    }
                }
            }

            if(BusinessConstants.STATUS_YES.equals(goodsSaledoGongxu.getIslast())){
                List<FactoryProduceSale> factoryProduceSaleList=iFactoryProduceSaleService.selectByParentId(goodsSaledoGongxu.getSaleId());
                if(factoryProduceSaleList.size()>0){
                    for(FactoryProduceSale factoryProduceSale:factoryProduceSaleList){
                        GoodsSale goodsSale=goodsSaleService.selectById(factoryProduceSale.getSaleId());
                        if(goodsSale!=null && goodsSale.getOrderCount()!=null){
                            if(goodsSaledoGongxu.getFinishCount().compareTo(goodsSale.getOrderCount())>=0){
                                goodsSale.setStatus(BusinessConstants.SALE_TYPE_15);
                                factoryProduceSale.setStatus(BusinessConstants.PROD_STATUS_5);
                            }else{
                                goodsSale.setStatus(BusinessConstants.SALE_TYPE_14);
                                factoryProduceSale.setStatus(BusinessConstants.PROD_STATUS_4);
                            }
                        }
                        iFactoryProduceSaleService.update(factoryProduceSale);
                    }
                }
            }
        }
        entity.setStatus(BusinessConstants.BAOGONG_STATUS1);
        /*entity.setUserId(goodsSaledoGongxuUser.getUserId());
        entity.setUserName(goodsSaledoGongxuUser.getUserName());*/
        SysUser sysUser=sysUserService.selectUserById(goodsSaledoGongxuUser.getUserId());
        if(sysUser!=null){
            entity.setUserId(sysUser.getUserId());
            entity.setUserName(sysUser.getUserName());
            if(StringUtils.isNotBlank(sysUser.getDeptId())){
                SysDept sysDept=iSysDeptService.selectDeptById(sysUser.getDeptId());
                if(sysDept!=null){
                    entity.setDeptName(sysDept.getDeptName());
                }
            }
        }
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setXuhao(goodsSaledoGongxuUser.getXuhao());
        entity.setIslast(goodsSaledoGongxuUser.getIslast());
        //entity.setDoTime(new Date());
        entity.setDoTime(entity.getDoTime());
        return goodsSaledoGongxuUserMoneyMapper.insert(entity);
    }

    @Override
    public int addBaogongHebing(GoodsSaledoGongxuUserMoney entity) {
        String[] ids=entity.getParentIds().split(",");
        for(String parentId:ids) {
            GoodsSaledoGongxuUser goodsSaledoGongxuUser = goodsSaledoGongxuUserService.selectById(parentId);
            BigDecimal remainCount=goodsSaledoGongxuUser.getRemainCount();
            goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().add(goodsSaledoGongxuUser.getRemainCount()));
            goodsSaledoGongxuUser.setRemainCount(BigDecimal.ZERO);
            goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
            GoodsSaledoGongxu goodsSaledoGongxu = goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
            if (goodsSaledoGongxu != null) {
                goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().add(goodsSaledoGongxuUser.getRemainCount()));
                goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().subtract(goodsSaledoGongxuUser.getRemainCount()));
                goodsSaledoGongxuService.update(goodsSaledoGongxu);

                GoodsSaledoGongxu goodsSaledoGongxuLast = goodsSaledoGongxuService.selectLastFinish(goodsSaledoGongxu.getSaleId());
                if (goodsSaledoGongxuLast != null) {
                    if (goodsSaledoGongxuLast.getFinishCount() != null) {
                        if (goodsSaledoGongxuLast.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                            FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                            factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuLast.getFinishCount());
                            factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuLast.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                            if (factoryProduceSaleAll.getFinishdoCount().compareTo(factoryProduceSaleAll.getCount()) >= 0) {
                                factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_5);
                            } else {
                                factoryProduceSaleAll.setStatus(BusinessConstants.PROD_STATUS_4);
                            }
                            iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                            List<FactoryProduceSaleAll> list = iFactoryProduceSaleAllService.selectHaveNoFinish(factoryProduceSaleAll.getProduceId());
                            FactoryProduceMain factoryProduceMain = iFactoryProduceMainService.selectById(factoryProduceSaleAll.getProduceId());
                            if (factoryProduceMain != null) {
                                if (list.size() > 0) {
                                    factoryProduceMain.setStatus(BusinessConstants.PROD_STATUS_4);
                                } else {
                                    factoryProduceMain.setStatus(BusinessConstants.PROD_STATUS_5);
                                }
                            }
                        }
                    }
                }

                if (BusinessConstants.STATUS_YES.equals(goodsSaledoGongxu.getIslast())) {
                    List<FactoryProduceSale> factoryProduceSaleList = iFactoryProduceSaleService.selectByParentId(goodsSaledoGongxu.getSaleId());
                    if (factoryProduceSaleList.size() > 0) {
                        for (FactoryProduceSale factoryProduceSale : factoryProduceSaleList) {
                            GoodsSale goodsSale = goodsSaleService.selectById(factoryProduceSale.getSaleId());
                            if (goodsSale != null && goodsSale.getOrderCount() != null) {
                                if (goodsSaledoGongxu.getFinishCount().compareTo(goodsSale.getOrderCount()) >= 0) {
                                    goodsSale.setStatus(BusinessConstants.SALE_TYPE_15);
                                    factoryProduceSale.setStatus(BusinessConstants.PROD_STATUS_5);
                                } else {
                                    goodsSale.setStatus(BusinessConstants.SALE_TYPE_14);
                                    factoryProduceSale.setStatus(BusinessConstants.PROD_STATUS_4);
                                }
                            }
                            iFactoryProduceSaleService.update(factoryProduceSale);
                        }
                    }
                }
            }
            GoodsSaledoGongxuUserMoney  entity1 = new GoodsSaledoGongxuUserMoney();
            entity1.setStatus(BusinessConstants.BAOGONG_STATUS1);
            SysUser sysUser = sysUserService.selectUserById(goodsSaledoGongxuUser.getUserId());
            if (sysUser != null) {
                entity1.setUserId(sysUser.getUserId());
                entity1.setUserName(sysUser.getUserName());
                if (StringUtils.isNotBlank(sysUser.getDeptId())) {
                    SysDept sysDept = iSysDeptService.selectDeptById(sysUser.getDeptId());
                    if (sysDept != null) {
                        entity1.setDeptName(sysDept.getDeptName());
                    }
                }
            }
            if (StringUtils.isBlank(entity1.getId())) {
                entity1.setId(IdUtils.fastSimpleUUID());
            }
            entity1.setCreateBy(SecurityUtils.getUsername());
            entity1.setCreateTime(DateUtils.getNowDate());
            entity1.setXuhao(goodsSaledoGongxuUser.getXuhao());
            entity1.setIslast(goodsSaledoGongxuUser.getIslast());
            entity1.setDoTime(entity.getDoTime());
            entity1.setParentId(parentId);
            entity1.setCount(remainCount);
            entity1.setMoney(goodsSaledoGongxuUser.getMoney());
            entity1.setAllMoney(remainCount.multiply(goodsSaledoGongxuUser.getMoney()));
            goodsSaledoGongxuUserMoneyMapper.insert(entity1);
        }
        return 1;
    }
    /**
     * 修改报工列
     * @param entity 报工列
     * @return 结果
     */
    @Override
    public int update(GoodsSaledoGongxuUserMoney entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaledoGongxuUserMoneyMapper.update(entity);
    }

    @Override
    public int editBaogong(GoodsSaledoGongxuUserMoney entity) {
        GoodsSaledoGongxuUserMoney goodsSaledoGongxuUserMoney=goodsSaledoGongxuUserMoneyMapper.selectById(entity.getId());
        GoodsSaledoGongxuUser goodsSaledoGongxuUser =goodsSaledoGongxuUserService.selectById(entity.getParentId());
        goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().add(entity.getCount()).subtract(goodsSaledoGongxuUserMoney.getCount()));
        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getCount().subtract(goodsSaledoGongxuUser.getFinishCount()));
        if(goodsSaledoGongxuUser.getRemainCount().compareTo(BigDecimal.ZERO) > 0){
            goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_1);
        }else{
            goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_2);
        }
        goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
        if(goodsSaledoGongxu!=null) {
            goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().add(entity.getCount()).subtract(goodsSaledoGongxuUserMoney.getCount()));
            goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getCounts().subtract(goodsSaledoGongxu.getFinishCount()));
            goodsSaledoGongxuService.update(goodsSaledoGongxu);
        }
        if(StringUtils.isNotBlank(goodsSaledoGongxu.getSaleId())){
            //所有工序都完成才可
           /* GoodsSaledoGongxu goodsSaledoGongxuMin= goodsSaledoGongxuService.selectMinFinish(goodsSaledoGongxu.getSaleId());
            if(goodsSaledoGongxuMin!=null){
                if(goodsSaledoGongxuMin.getFinishCount()!=null) {
                    if (goodsSaledoGongxuMin.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                        FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuMin.getFinishCount());
                        factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuMin.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                        iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                    }
                }
            }*/
            GoodsSaledoGongxu goodsSaledoGongxuLast= goodsSaledoGongxuService.selectLastFinish(goodsSaledoGongxu.getSaleId());
            if(goodsSaledoGongxuLast!=null){
                if(goodsSaledoGongxuLast.getFinishCount()!=null) {
                    if (goodsSaledoGongxuLast.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                        FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuLast.getFinishCount());
                        factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuLast.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                        iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                    }
                }
            }
        }

        entity.setDoTime(new Date());
        SysUser sysUser=sysUserService.selectUserById(goodsSaledoGongxuUser.getUserId());
        if(sysUser!=null){
            entity.setUserId(sysUser.getUserId());
            entity.setUserName(sysUser.getUserName());
            if(StringUtils.isNotBlank(sysUser.getDeptId())){
                SysDept sysDept=iSysDeptService.selectDeptById(sysUser.getDeptId());
                if(sysDept!=null){
                    entity.setDeptName(sysDept.getDeptName());
                }
            }
        }
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaledoGongxuUserMoneyMapper.update(entity);
    }

    @Override
    public int editZhijian(GoodsSaledoGongxuUserMoney entity) {
        entity.setZhijianId(SecurityUtils.getLoginUser().getUser().getUserId());
        entity.setZhijianUser(SecurityUtils.getLoginUser().getUser().getUserName());
        entity.setZhijianDate(DateUtils.getNowDate());
        entity.setSuccessCount(entity.getCount().subtract(entity.getErrorCount()));
        entity.setSuccessLv(entity.getSuccessCount().multiply(BigDecimal.valueOf(100)).divide(entity.getCount(),2,BigDecimal.ROUND_HALF_UP));
        entity.setStatus(BusinessConstants.BAOGONG_STATUS2);
        entity.setAllMoney(entity.getSuccessCount().multiply(entity.getMoney()));
        goodsSaledoGongxuUserMoneyMapper.update(entity);
        GoodsSaledoGongxuUser goodsSaledoGongxuUser= goodsSaledoGongxuUserService.selectById(entity.getParentId());
        if(goodsSaledoGongxuUser!=null){
            goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().subtract(entity.getErrorCount()));
            goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getCount().subtract(goodsSaledoGongxuUser.getFinishCount()));
            goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);

            GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
            if(goodsSaledoGongxu!=null) {
                goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().subtract(entity.getErrorCount()));
                goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().add(entity.getErrorCount()));
                goodsSaledoGongxuService.update(goodsSaledoGongxu);
            }

            //计算
            GoodsSaledoGongxuUserLirun goodsSaledoGongxuUserLirun=new GoodsSaledoGongxuUserLirun();
            goodsSaledoGongxuUserLirun.setProjectId(entity.getProjectId());
            goodsSaledoGongxuUserLirun.setSaleId(entity.getSaleId());
            goodsSaledoGongxuUserLirun.setGoodId(entity.getGoodsId());
            goodsSaledoGongxuUserLirun.setGongxuId(entity.getGongxuId());
            List<GoodsSaledoGongxuUserLirun> goodsSaledoGongxuUserLirunList=goodsSaledoGongxuUserLirunService.selectList(goodsSaledoGongxuUserLirun);
            if(goodsSaledoGongxuUserLirunList.size()>0){

            } else {
                    /*if(StringUtils.isNotBlank(entity.getProjectId())){
                        for(String projectId:entity.getProjectId().split(",")){
                            ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(projectId);
                            projectBaseInfo.setBuyMoney(projectBaseInfo.getBackMoney().add());
                        }
                    }*/
                List<FactoryProduceSale> factoryProduceSaleList=iFactoryProduceSaleService.selectByParentId(entity.getSaleId());
                for(FactoryProduceSale factoryProduceSale:factoryProduceSaleList){
                    GoodsSale goodsSale=goodsSaleService.selectById(factoryProduceSale.getSaleId());
                    if(goodsSale!=null){
                        ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(goodsSale.getProjectId());
                        projectBaseInfo.setBuyMoney(projectBaseInfo.getBuyMoney().add(entity.getMoney().multiply(goodsSale.getOrderCount())));
                        projectBaseInfo.setProfitMoney(projectBaseInfo.getBackMoney().subtract(projectBaseInfo.getBuyMoney()).subtract(projectBaseInfo.getFeeMoney()));
                        projectBaseInfoService.updateBackMoney(projectBaseInfo);

                        GoodsSaledoGongxuUserLirun goodsSaledoGongxuUserLirun1=new GoodsSaledoGongxuUserLirun();
                        BeanUtils.copyProperties(entity, goodsSaledoGongxuUserLirun1, "id");
                        goodsSaledoGongxuUserLirun1.setProjectId(entity.getProjectId());
                        goodsSaledoGongxuUserLirun1.setSaleId(entity.getSaleId());
                        goodsSaledoGongxuUserLirun1.setGoodId(entity.getGoodsId());
                        goodsSaledoGongxuUserLirun1.setGongxuId(entity.getGongxuId());
                        goodsSaledoGongxuUserLirun1.setSaleCount(goodsSale.getOrderCount());
                        goodsSaledoGongxuUserLirun1.setName(entity.getName());
                        goodsSaledoGongxuUserLirun1.setGongxuName(entity.getGongxuName());
                        goodsSaledoGongxuUserLirun1.setGongxuModel(entity.getGongxuModel());
                        goodsSaledoGongxuUserLirun1.setSaleMoney(entity.getMoney().multiply(goodsSale.getOrderCount()));
                        goodsSaledoGongxuUserLirunService.insert(goodsSaledoGongxuUserLirun1);
                    }
                }
            }

        }
        return 1;
    }

    /**
     * 批量删除报工列
     * @param ids 需要删除的报工列主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSaledoGongxuUserMoneyMapper.deleteByIds(ids);
    }

    /**
     * 删除报工列信息
     * @param id 报工列主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSaledoGongxuUserMoneyMapper.deleteById(id);
    }

    @Override
    public int deleteByIdsUpdate(String id) {
        GoodsSaledoGongxuUserMoney entity=goodsSaledoGongxuUserMoneyMapper.selectById(id);
        GoodsSaledoGongxuUser goodsSaledoGongxuUser =goodsSaledoGongxuUserService.selectById(entity.getParentId());
        goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().subtract(entity.getCount()));
        goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getRemainCount().add(entity.getCount()));
        if(goodsSaledoGongxuUser.getRemainCount().compareTo(BigDecimal.ZERO) <= 0){
            goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_2);
        }else{
            if(goodsSaledoGongxuUser.getFinishCount().compareTo(BigDecimal.ZERO)==0){
                goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_0);
            }else{
                goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_1);
            }
        }
        goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
       /* if(goodsSaledoGongxu!=null){*/
            goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().subtract(entity.getCount()));
            goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().add(entity.getCount()));
            goodsSaledoGongxuService.update(goodsSaledoGongxu);
    /*    }*/

        /*GoodsSaledoGongxu goodsSaledoGongxuMin= goodsSaledoGongxuService.selectMinFinish(goodsSaledoGongxu.getSaleId());
        if(goodsSaledoGongxuMin!=null){
            if(goodsSaledoGongxuMin.getFinishCount()!=null) {
                if (goodsSaledoGongxuMin.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                    GoodsSale goodsSale = goodsSaleService.selectById(goodsSaledoGongxu.getSaleId());
                    goodsSale.setFinishdoCount(goodsSaledoGongxuMin.getFinishCount());
                    goodsSale.setKerukuCount(goodsSaledoGongxuMin.getFinishCount().subtract(goodsSale.getRukuCount()));
                    goodsSaleService.update(goodsSale);
                }
            }
        }*/
        GoodsSaledoGongxu goodsSaledoGongxuLast= goodsSaledoGongxuService.selectLastFinish(goodsSaledoGongxu.getSaleId());
        if(goodsSaledoGongxuLast!=null){
            if(goodsSaledoGongxuLast.getFinishCount()!=null) {
                if (goodsSaledoGongxuLast.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                    FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                    factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuLast.getFinishCount());
                    factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuLast.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                    iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                }
            }
        }
        return goodsSaledoGongxuUserMoneyMapper.deleteById(id);
    }

    @Override
    public int zhijianAllUpdate(String[] ids) {
        for(String id:ids) {
            GoodsSaledoGongxuUserMoney entity = goodsSaledoGongxuUserMoneyMapper.selectById(id);
            if(BusinessConstants.BAOGONG_STATUS1.equals(entity.getStatus())) {
                entity.setZhijianId(SecurityUtils.getLoginUser().getUser().getUserId());
                entity.setZhijianUser(SecurityUtils.getLoginUser().getUser().getUserName());
                entity.setZhijianDate(DateUtils.getNowDate());
                entity.setSuccessCount(entity.getCount());
                entity.setErrorCount(BigDecimal.ZERO);
                entity.setSuccessLv(entity.getSuccessCount().multiply(BigDecimal.valueOf(100)).divide(entity.getCount(), 2, BigDecimal.ROUND_HALF_UP));
                entity.setStatus(BusinessConstants.BAOGONG_STATUS2);
                entity.setAllMoney(entity.getSuccessCount().multiply(entity.getMoney()));
                goodsSaledoGongxuUserMoneyMapper.update(entity);
                GoodsSaledoGongxuUser goodsSaledoGongxuUser=goodsSaledoGongxuUserService.selectById(entity.getParentId());
                if(goodsSaledoGongxuUser!=null){
                    if(goodsSaledoGongxuUser.getFinishCount().compareTo(BigDecimal.ZERO)==0){
                        goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_0);
                        goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
                    }else{
                        goodsSaledoGongxuUser.setStatus(BusinessConstants.GONGXU_BAOGONG_1);
                        goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);
                    }
                }
                //计算
                GoodsSaledoGongxuUserLirun goodsSaledoGongxuUserLirun=new GoodsSaledoGongxuUserLirun();
                goodsSaledoGongxuUserLirun.setProjectId(entity.getProjectId());
                goodsSaledoGongxuUserLirun.setSaleId(entity.getSaleId());
                goodsSaledoGongxuUserLirun.setGoodId(entity.getGoodsId());
                goodsSaledoGongxuUserLirun.setGongxuId(entity.getGongxuId());
                List<GoodsSaledoGongxuUserLirun> goodsSaledoGongxuUserLirunList=goodsSaledoGongxuUserLirunService.selectList(goodsSaledoGongxuUserLirun);
                if(goodsSaledoGongxuUserLirunList.size()>0){

                } else {
                    /*if(StringUtils.isNotBlank(entity.getProjectId())){
                        for(String projectId:entity.getProjectId().split(",")){
                            ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(projectId);
                            projectBaseInfo.setBuyMoney(projectBaseInfo.getBackMoney().add());
                        }
                    }*/
                    List<FactoryProduceSale> factoryProduceSaleList=iFactoryProduceSaleService.selectByParentId(entity.getSaleId());
                    for(FactoryProduceSale factoryProduceSale:factoryProduceSaleList){
                        GoodsSale goodsSale=goodsSaleService.selectById(factoryProduceSale.getSaleId());
                        if(goodsSale!=null){
                            ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(goodsSale.getProjectId());
                            projectBaseInfo.setBuyMoney(projectBaseInfo.getBuyMoney().add(entity.getMoney().multiply(goodsSale.getOrderCount())));
                            projectBaseInfo.setProfitMoney(projectBaseInfo.getBackMoney().subtract(projectBaseInfo.getBuyMoney()).subtract(projectBaseInfo.getFeeMoney()));
                            projectBaseInfoService.updateBackMoney(projectBaseInfo);

                            GoodsSaledoGongxuUserLirun goodsSaledoGongxuUserLirun1=new GoodsSaledoGongxuUserLirun();
                            BeanUtils.copyProperties(entity, goodsSaledoGongxuUserLirun1, "id");
                            goodsSaledoGongxuUserLirun1.setProjectId(entity.getProjectId());
                            goodsSaledoGongxuUserLirun1.setSaleId(entity.getSaleId());
                            goodsSaledoGongxuUserLirun1.setGoodId(entity.getGoodsId());
                            goodsSaledoGongxuUserLirun1.setGongxuId(entity.getGongxuId());
                            goodsSaledoGongxuUserLirun1.setSaleCount(goodsSale.getOrderCount());
                            goodsSaledoGongxuUserLirun1.setName(entity.getName());
                            goodsSaledoGongxuUserLirun1.setGongxuName(entity.getGongxuName());
                            goodsSaledoGongxuUserLirun1.setGongxuModel(entity.getGongxuModel());
                            goodsSaledoGongxuUserLirun1.setSaleMoney(entity.getMoney().multiply(goodsSale.getOrderCount()));
                            goodsSaledoGongxuUserLirunService.insert(goodsSaledoGongxuUserLirun1);
                        }
                    }
                }
            }
        }
        return ids.length;
    }

    @Override
    public int zhijianAllNoUpdate(String[] ids) {
        for(String id:ids) {
            GoodsSaledoGongxuUserMoney entity = goodsSaledoGongxuUserMoneyMapper.selectById(id);
            if(BusinessConstants.BAOGONG_STATUS1.equals(entity.getStatus())) {
                entity.setZhijianId(SecurityUtils.getLoginUser().getUser().getUserId());
                entity.setZhijianUser(SecurityUtils.getLoginUser().getUser().getUserName());
                entity.setZhijianDate(DateUtils.getNowDate());
                entity.setSuccessCount(BigDecimal.ZERO);
                entity.setErrorCount(entity.getCount());
                entity.setSuccessLv(entity.getSuccessCount().multiply(BigDecimal.valueOf(100)).divide(entity.getCount(), 2, BigDecimal.ROUND_HALF_UP));
                entity.setStatus(BusinessConstants.BAOGONG_STATUS2);
                entity.setAllMoney(entity.getSuccessCount().multiply(entity.getMoney()));
                goodsSaledoGongxuUserMoneyMapper.update(entity);
                GoodsSaledoGongxuUser goodsSaledoGongxuUser= goodsSaledoGongxuUserService.selectById(entity.getParentId());
                if(goodsSaledoGongxuUser!=null){
                    goodsSaledoGongxuUser.setFinishCount(goodsSaledoGongxuUser.getFinishCount().subtract(entity.getCount()));
                    goodsSaledoGongxuUser.setRemainCount(goodsSaledoGongxuUser.getCount().subtract(goodsSaledoGongxuUser.getFinishCount()));
                    goodsSaledoGongxuUserService.update(goodsSaledoGongxuUser);

                    GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(goodsSaledoGongxuUser.getParentId());
                    if(goodsSaledoGongxu!=null) {
                        goodsSaledoGongxu.setFinishCount(goodsSaledoGongxu.getFinishCount().subtract(entity.getCount()));
                        goodsSaledoGongxu.setRemainfinishCount(goodsSaledoGongxu.getRemainfinishCount().add(entity.getCount()));
                        goodsSaledoGongxuService.update(goodsSaledoGongxu);
                    }

                }
            }
        }
        return ids.length;
    }
    @Override
    public int updateStatusByParentId(String status,String parentId) {
        return goodsSaledoGongxuUserMoneyMapper.updateStatusByParentId(status,parentId);
    }
    @Override
    public void updateAllNotLast(String gongyiId) {
        goodsSaledoGongxuUserMoneyMapper.updateAllNotLast(gongyiId);
    }
    @Override
    public void updateLastById(String id) {
        goodsSaledoGongxuUserMoneyMapper.updateLastById( id);
    }
}

